package com.cc.controller;

import com.cc.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {
    //GET请求单参数
    @GetMapping("/getParam")
    public String getParam(String name) {
        System.out.println(name);
        return name;
    }

    //GET请求多参数
    @GetMapping("/getParams")
    public String getParams(@RequestParam("userName") String name, Integer age, String address) {
        System.out.println("普通参数传递 name ==>" + name);
        System.out.println("普通参数传递 age ==>" + age);
        System.out.println("普通参数传递 address ==>" + address);
        return name;
    }

    //接收pojo对象
    @GetMapping("getPojoParams")
    public String postPojoParams(User user) {
        System.out.println("接收pojo参数 user ==>" + user);
        System.out.println("接收pojo嵌套参数 role ==>" + user.getRole());
        return "参数返回接收成功!";
    }

    //接收数组参数
    @GetMapping("getArrayParams")
    public String getArrayParams(String[] hobbies) {
        System.out.println("数组参数传递 hobbies ==>" + Arrays.toString(hobbies));
        return "接收数组参数成功!";
    }

    //接收集合参数
    @GetMapping("listParams")
    public String listParams(@RequestParam List<String> hobbies) {
        System.out.println("数组参数传递 hobbies ==>" + hobbies);
        return "接收数组参数成功!";
    }

    //接收前端json数据
    @GetMapping("jsonParams")
    public String jsonParams(@RequestBody User user) {
        System.out.println("json数据传递 ==>" + user);
        System.out.println("json数据传递 ==>" + user.getRole());
        return "接收前端json数据成功!";
    }

    //接收json数组
    @PostMapping("postJsonParams")
    public String postJsonParams(@RequestBody List<User> users){

        System.out.println("json数据传递 ==>" + users);

        return "接收json数组";
    }
}
